

package com.maoshi.shop.distribution.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.maoshi.shop.common.config.Constant;
import com.maoshi.shop.common.exception.MaoshiShopBindException;
import com.maoshi.shop.distribution.comment.dto.DistributionLevelDto;
import com.maoshi.shop.distribution.comment.model.DistributionLevelAuditing;
import com.maoshi.shop.distribution.comment.model.DistributionLevelSet;
import com.maoshi.shop.distribution.comment.model.DistributionUser;
import com.maoshi.shop.distribution.comment.service.DistributionLevelAuditingService;
import com.maoshi.shop.distribution.comment.service.DistributionLevelService;
import com.maoshi.shop.distribution.comment.service.DistributionLevelSetService;
import com.maoshi.shop.distribution.comment.service.DistributionUserService;
import com.maoshi.shop.distribution.comment.vo.DistributionLevelConditionsSwitchVO;
import com.maoshi.shop.security.api.model.MaoshiUser;
import com.maoshi.shop.security.api.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * 分销等级接口
 */
@RestController
@RequestMapping("/p/distribution/level")
@Api(tags = "分销等级接口")
@AllArgsConstructor
public class DistributionLevelController {

    private final DistributionLevelService distributionLevelService;

    private final DistributionLevelSetService distributionLevelSetService;

    private final DistributionUserService distributionUserService;

    private final DistributionLevelAuditingService distributionLevelAuditingService;

    private final MapperFacade mapperFacade;



    @GetMapping("/page")
    @ApiOperation(value = "查看店铺设置的等级", notes = "查看店铺设置的等级")
    public ResponseEntity<List<DistributionLevelDto>> page() {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(SecurityUtils.getUser().getUserId(), shopId);


        DistributionLevelSet distributionLevelSet = distributionLevelSetService.getByShopId();
        List<DistributionLevelDto> distributionLevels = mapperFacade.mapAsList(distributionLevelSet.getDistributionLevels(), DistributionLevelDto.class);

        Integer level = distributionUser.getLevel();
        distributionLevels.forEach(distributionLevelDto -> {
            distributionLevelDto.setIsCurrentLevel(Objects.equals(distributionLevelDto.getLevel(), level));
            distributionLevelDto.setLevelSetConditionsSwitch(distributionLevelSet.getLevelSetConditionsSwitch());
        });
        return ResponseEntity.ok(distributionLevels);
    }

    @PostMapping("/upgrade")
    @ApiOperation(value = "申请升级", notes = "根据店铺id申请")
    public ResponseEntity<Void> upgrade() {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        MaoshiUser user = SecurityUtils.getUser();
        String userId = user.getUserId();

        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(userId, shopId);

        if (distributionUser == null || !Objects.equals(distributionUser.getState(), 1)) {
            throw new MaoshiShopBindException("你还不是分销员，无法进行升级操作");
        }

        DistributionLevelSet distributionLevelSet = distributionLevelSetService.getByShopId();

        // 当前用户等级
        Integer level = distributionUser.getLevel();

        if (distributionLevelSet.getDistributionLevels().size() <= level) {
            throw new MaoshiShopBindException("已达到等级上限");
        }

        distributionLevelService.tryingToUpgrade(distributionUser, distributionLevelSet);

        distributionUserService.removeCacheByUserIdAndShopId(userId, shopId);
        return ResponseEntity.ok().build();
    }


}
